//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ybwh.springboot2.anno;




import com.alibaba.fastjson.JSON;
import com.google.common.collect.Sets;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Set;

public class HandlerAnnotationParser implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
    private static final Logger log = LoggerFactory.getLogger(HandlerAnnotationParser.class);
    private static final Set<HandlerWrapper> cache = Sets.newConcurrentHashSet();
    private ResourceLoader resourceLoader;

    public HandlerAnnotationParser() {
    }


    @Override
    public void setResourceLoader(@NonNull ResourceLoader resourceLoader) {
        if (resourceLoader == null) {
            throw new NullPointerException("resourceLoader is marked @NonNull but is null");
        } else {
            this.resourceLoader = resourceLoader;
        }
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
        if (registry == null) {
            throw new NullPointerException("registry is marked @NonNull but is null");
        } else {
            AnnotationAttributes mapperScanAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(HandlerScan.class.getName()));
            if (mapperScanAttrs != null) {
                String[] basePackages = mapperScanAttrs.getStringArray("basePackages");
                HandlerAnnotationScanner scanner = new HandlerAnnotationScanner(registry);
                scanner.setBeanNameGenerator((beanDefinition, beanDefinitionRegistry) -> {
                    String beanClassName = beanDefinition.getBeanClassName();

                    try {
                        Class<?> clz = Class.forName(beanClassName);
                        Handler handler = (Handler)clz.getAnnotation(Handler.class);
                        if (null == handler) {
                            return null;
                        } else {
                            if (null != handler.targets() && handler.targets().length > 0) {
                                String[] targets = handler.targets();
                                int len = targets.length;

                                for(int i = 0; i < len; ++i) {
                                    String targetStr = targets[i];
                                    HandlerWrapper wrapper = JSON.parseObject(targetStr, HandlerWrapper.class);
                                    wrapper.setHandler(beanClassName);
                                    wrapper.setName(handler.name());
                                    wrapper.setOwner(handler.owner());
                                    cache.add(wrapper);
                                }
                            }

                            return beanClassName;
                        }
                    } catch (ClassNotFoundException var11) {
                        log.error(var11.getMessage(), var11);
                        return null;
                    }
                });


                if (this.resourceLoader != null) {
                    scanner.setResourceLoader(this.resourceLoader);
                }

                scanner.doScan(basePackages);
            }
        }
    }

    public static Set<HandlerWrapper> getFromCache() {
        return cache;
    }

    public static void clearCache() {
        cache.clear();
    }

}
